package com.yiren.dbaa.module.practice_set.line.view.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.yiren.backstage.config.api.LoadConfigApi;
import com.yiren.backstage.config.bean.device.UserParamConfigBean;
import com.yiren.backstage.currency.entity.LineInfoEntity;
import com.yiren.dbaa.R;
import com.yiren.dbaa.activity.BaseActivity;
import com.yiren.dbaa.currency.dialog.CommonDialog;
import com.yiren.dbaa.module.practice_set.line.contract.LineHistoryContract;
import com.yiren.dbaa.module.practice_set.line.presenter.LineHistoryPresenter;
import com.yiren.dbaa.module.practice_set.line.view.adapter.LineVersionAdapter;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author lwb
 * @date : 2022/11/16 17:44
 * @description: 线路历史版本
 */
public class LineHistoryActivity extends BaseActivity implements LineHistoryContract.View {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.ll_no_data)
    LinearLayout llNoData;
    @BindView(R.id.tv_tips)
    TextView tvTips;
    @BindView(R.id.lv_version_line)
    ListView lvVersionLine;

    private LineInfoEntity line;
    private LineHistoryContract.Presenter versionHistoryPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history_line);
        ButterKnife.bind(this);
        tvTitle.setText("线路回退版本");
        line = (LineInfoEntity) this.getIntent().getSerializableExtra("line");
        init();
    }

    private void init() {
        versionHistoryPresenter = new LineHistoryPresenter(this);
        UserParamConfigBean userParamConfigBean = LoadConfigApi.loadUserParamConfig(this);
        if (line != null) {
            versionHistoryPresenter.loadVersionList(this, userParamConfigBean.getUserInfo().getId().toString(), line);
        }
    }

    @OnClick(R.id.iv_back)
    public void onClick() {
        finish();
    }

    @Override
    public void showVersionList(List<LineInfoEntity> lineList) {
        if (lineList.size() == 0) {
            llNoData.setVisibility(View.VISIBLE);
            lvVersionLine.setVisibility(View.GONE);
            tvTips.setText("当前暂无历史版本数据!");
        } else {
            llNoData.setVisibility(View.GONE);
            lvVersionLine.setVisibility(View.VISIBLE);
            LineVersionAdapter versionAdapter = new LineVersionAdapter(this, lineList);
            lvVersionLine.setAdapter(versionAdapter);
            versionAdapter.setOnManageClickListener(position -> {
                CommonDialog.showTwoBtn(this, "操作确认", "下载会覆盖本地考规，确定下载吗？", "取消", "下载", (dialog, which) -> {
                    if (which == -2) {
                        dialog.dismiss();
                    } else {
                        lineList.get(position).setSiteCode(line.getSiteCode());
                        lineList.get(position).setName(line.getName());
                        lineList.get(position).setLineVerLast(line.getLineVerLast());
                        lineList.get(position).setLocation(line.getLocation());
                        versionHistoryPresenter.downLoadLineData(lineList.get(position), this);
                        dialog.dismiss();
                    }
                });
            });
        }
    }

}